package cn.gok.demo.controller;


import cn.gok.demo.dao.StudentMapper;
import cn.gok.demo.entity.Student;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyx
 * @since 2022-07-22
 */




@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentMapper studentMapper;
    //1）	查询所有学生信息
    @RequestMapping("selectallstudent")
    public List<Student> selectallstudent(){

        return studentMapper.selectList(null);
    }
    //        2）	根据ID查询学生信息
    @RequestMapping("selectById")
    public Student selectById(@RequestParam long sid){
        return studentMapper.selectById(sid);
    }
    //        3）	模糊查询学生信息
    @RequestMapping("selectlike")
    public List<Student> selectlike(Student student){
        QueryWrapper<Student> wrapper=new QueryWrapper<>();
        wrapper.like(student.getSid()!=null,"sname",student.getSid());
        wrapper.like(student.getSname()!=null,"sname",student.getSname());
        wrapper.like(student.getSex()!=null,"sname",student.getSex());
        wrapper.like(student.getSnum()!=null,"sname",student.getSnum());
        return studentMapper.selectList(wrapper);
    }
//        4）	分页查询学生信息
    @RequestMapping("selectpage")
    public IPage<Student> selectpage(@RequestParam int cur, @RequestParam int size){
        Page<Student> page=new Page<>(cur,size);
        page.getRecords().forEach(System.out::println);
        System.out.println(page.getTotal());
        return studentMapper.selectPage(page,null);
    }

    //        5）	添加学生信息
    @RequestMapping("insertstudent")
    public int insertstudent(Student student){
        return studentMapper.insert(student);
    }
    //        6）	修改学生信息

    @RequestMapping("updatestudent")
    public int updatestudent(Student student){
        return studentMapper.updateById(student);
    }

    //        7）	根据ID删除学生
    @RequestMapping("deleteById")
    public int deleteById(@RequestParam long sid){
        return studentMapper.deleteById(sid);
    }

    //        8）	批量删除学生
    @RequestMapping("deleteByIds")
    public int deleteByIds(@RequestParam List<String> sid){
        return studentMapper.deleteBatchIds(sid);
    }

}

